home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8899 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: do || die;
  5. Date: 7 Mar 1996 01:32:18 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hmaf2INNqir@keats.ugrad.cs.ubc.ca>
  8. References: <1996Mar7.052636.59812@ucl.ac.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <1996Mar7.052636.59812@ucl.ac.uk>,
  12. Timothy Slidel <slidel@bsm.bioc.ucl.ac.uk> wrote:
  13. >Whilst it seems to work ok - is it considered bad style to use
  14. >logical operator expressions as conditional statements on their own, a la
  15. >Perl?
  16.  
  17. Style is akin to taste and aesthetics. There are some rough guidelines, sort of
  18. like elusive artistic _faux pas_, which distinguish obfuscated C from readable,
  19. maintainable C.
  20.  
  21. >e.g. if I want to decrement i only when it is != 0:
  22. >
  23. >i && i--;
  24. >
  25. >clearly something like:
  26. >
  27. >i && continue;
  28. >
  29. >won't work because continue is a keyword.
  30. >
  31. >I couldn't find anything about this in K&R2 or the C-FAQ and gcc -Wall just
  32.  
  33. I did. I found out from the C grammar listed in the K&R that a the
  34. ``statement'' syntactic unit generates a ``logical-AND-expression ; ''
  35. sentential form quite happily. :)
  36.  
  37. >issues a warning about an unused value...
  38.  
  39. ...which is somewhat of an indicator that all may not be well, stylistically.
  40. However, it is valid syntax, and the behavior is well defined. The evaluation
  41. order of && is left to right, guaranteed, so it is clear that the decrementing
  42. side-effect takes place as a very last step, conceptually.
  43.  
  44. Still, at first glance it appears as though undefined behavior is being
  45. invoked, until you remember the short-circuit evaluation of the && operator.
  46.  
  47. It might seem confusing at first to someone not accustomed to dealing with
  48. Bourne shell scripts or Perl. But once you get used to seeing the construct, it
  49. becomes perspicuous.
  50.  
  51. I think I did use it once or twice myself, with the || operator, but it is not
  52. likely that would have written that had I not previously learned Bourne shell
  53. scripting.
  54. -- 
  55.  
  56.